package org.xshop.web;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.xshop.model.TGoods;
import org.xshop.model.TGoodsLimit;
import org.xshop.model.TGoodsPromotion;
import org.xshop.model.TMainIndex;
import org.xshop.service.ITGoodsLimitService;
import org.xshop.service.ITGoodsService;
import org.xshop.service.ITMainIndexService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.ibase4j.core.base.AppBaseController;
import top.ibase4j.core.util.DataUtil;
import top.ibase4j.core.util.DateUtil;
import top.ibase4j.core.util.WebUtil;

/**
 * <p>
 * 收藏 前端控制器
 * </p>
 *
 * @author ZhuLin
 * @since 2017-6-15
 */
@Controller
@RequestMapping("/app/mainIndex/")
@Api(tags = "MainIndex", value = "首页接口", description = "APP-首页接口")
public class TMainIndexController extends AppBaseController<TMainIndex, ITMainIndexService> {

    @Autowired
    private ITGoodsLimitService goodsLimitService;
    @Autowired
    private ITGoodsService goodsService;

    @SuppressWarnings("unchecked")
    @ApiOperation(value = "首页列表", notes = "获取首页的列表内容", produces = MediaType.APPLICATION_JSON_VALUE, response = TMainIndex.class)
    @RequestMapping(value = "getList.api", method = {RequestMethod.GET, RequestMethod.POST})
    public Object query(HttpServletRequest request, String type) {
        Map<String, Object> param = WebUtil.getParameter(request);
        Long id = getCurrUser(request);
        if (DataUtil.isNotEmpty(id)) {
            param.put("memberId", id);
        }
        Object result = service.queryList(param);

        List<TMainIndex> items = (List<TMainIndex>)result;
        @SuppressWarnings("serial")
        Map<String, Object> root = new LinkedHashMap<String, Object>() {
            {
                put("banners",
                    items.stream().filter(item -> item.getType().equals(String.valueOf(ItemType.BANNERS.value)))
                    .collect(Collectors.toList()));
                put("panicBuying",
                    items.stream().filter(item -> item.getType().equals(String.valueOf(ItemType.PANIC_BUYING.value)))
                    .collect(Collectors.toList()));
                put("recommendedStore",
                    items.stream()
                    .filter(item -> item.getType().equals(String.valueOf(ItemType.RECOMMENDED_STORE.value)))
                    .collect(Collectors.toList()));
                put("specialPrice",
                    items.stream().filter(item -> item.getType().equals(String.valueOf(ItemType.SPECIAL_PRICE.value)))
                    .collect(Collectors.toList()));
                put("hotSold",
                    items.stream().filter(item -> item.getType().equals(String.valueOf(ItemType.HOT_SOLD.value)))
                    .collect(Collectors.toList()));

            }
        };
        // 限时抢购
        List<TMainIndex> panicBuying = (List<TMainIndex>)root.get("panicBuying");
        if (panicBuying != null) {
            for (int i = 0; i < panicBuying.size(); i++) {
                TMainIndex item = panicBuying.get(i);
                TGoodsLimit goodsLimit = new TGoodsLimit();
                goodsLimit.setGoodsId(item.getTypeId());
                goodsLimit = goodsLimitService.selectOne(goodsLimit);
                if (goodsLimit == null) {
                    panicBuying.remove(i);
                    i--;
                    continue;
                }
                Date today = new Date();
                if (today.before(goodsLimit.getStartTime()) || today.after(goodsLimit.getEndTime())) {
                    panicBuying.remove(i);
                    i--;
                    continue;
                }
                item.setStartTime(goodsLimit.getStartTime());
                item.setEndTime(goodsLimit.getEndTime());
            }
            root.put("panicBuying", panicBuying);
        }
        // 天天特价
        List<TMainIndex> specialPrice = (List<TMainIndex>)root.get("specialPrice");
        if (specialPrice != null) {
            for (int i = 0; i < specialPrice.size(); i++) {
                TMainIndex item = specialPrice.get(i);
                TGoods goods = goodsService.queryById(item.getTypeId());
                if (goods == null) {
                    specialPrice.remove(i);
                    i--;
                    continue;
                }
                if (goods.getIsPromotion() == 1) {
                    TGoodsPromotion promotion = goods.getPromotion();
                    Date now = DateUtil.stringToDate(DateUtil.getDate());
                    if (!promotion.getStartTime().after(now) && !promotion.getEndTime().before(now)) {
                        goods.setSalePrice(promotion.getPrice());
                    }
                }
                item.setGoods(goods);
            }
            root.put("specialPrice", specialPrice);
        }
        // 每周热卖
        List<TMainIndex> hotSold = (List<TMainIndex>)root.get("hotSold");
        if (hotSold != null) {
            for (int i = 0; i < hotSold.size(); i++) {
                TMainIndex item = hotSold.get(i);
                TGoods goods = goodsService.queryById(item.getTypeId());
                if (goods == null) {
                    hotSold.remove(i);
                    i--;
                    continue;
                }
                if (goods.getIsPromotion() == 1) {
                    TGoodsPromotion promotion = goods.getPromotion();
                    Date now = DateUtil.stringToDate(DateUtil.getDate());
                    if (!promotion.getStartTime().after(now) && !promotion.getEndTime().before(now)) {
                        goods.setSalePrice(promotion.getPrice());
                    }
                }
                item.setGoods(goods);
            }
            root.put("hotSold", hotSold);
        }
        return setSuccessModelMap(root);
    }

    private enum ItemType {
        BANNERS(0), PANIC_BUYING(1), RECOMMENDED_STORE(2), SPECIAL_PRICE(3), HOT_SOLD(4);
        private int value;

        ItemType(int value) {
            this.value = value;
        }
    }
}
